﻿Public Partial Class estimate
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            If Not (Session("Value") Is Nothing) Then
                'Dim lb As Label = CType(Master.FindControl("lbMenu"), Label)
                'lb.Text = "ค่าประมาณการ"
                If (Session("Status_Pay") & "" = "Y") Then
                    btnConfirm.Visible = False
                Else
                    btnConfirm.Visible = True
                End If
                If (Session("EditBooking_Show") & "" <> "") Then
                    btnConfirm.Visible = False
                End If
                Dim dsConfig As DataSet = Session("Config")
                Dim HTML As String = ""
                ViewState("VEHICLE_DETAIL") = Session("Value")
                Dim HeadReport As String = ""
                Dim Vat As String = ""
                Dim BuyerSeller As String = "N"
                Dim dt As DataTable = Session("Product_popup")
                For Each dr As DataRow In dt.Rows
                    If (clsGet.Get_IRPCBuyerSeller(dr.Item("Product_Buyer_no")) = "Y") Or (clsGet.Get_IRPCBuyerSeller(dr.Item("Product_Seller_no")) = "Y") Then
                        BuyerSeller = "Y"
                    End If
                Next
                If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                    Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                Else
                    Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                End If
                If (Session("IRPCLiquid") Is Nothing) Then
                    Session("IRPCLiquid") = ""
                End If
                If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(7) = "Draft") Then
                    HeadReport = dsConfig.Tables(0).Rows(0).Item("Head_Report_ComputeDraft")
                    lbEstimate.Text = "ESTIMATE"
                    lbEstimate.Visible = True
                    btnPrint.Visible = False
                    'ตัวอย่าง
                    If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                        'Liquid
                        Dim dtBody As DataTable = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "A", ViewState("VEHICLE_DETAIL").ToString().Split("$")(14)).Tables(0)
                        HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                        ' ViewState("CodeMatFree") รหัสของ MAT ที่ฟรี

                    Else
                        'Bulk
                        Dim dtBody As DataTable = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "A", ViewState("VEHICLE_DETAIL").ToString().Split("$")(14)).Tables(0)
                        HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                        For i As Integer = 1 To CType(Session("MAT_USE"), DataSet).Tables.Count - 1
                            If (CType(Session("MAT_USE"), DataSet).Tables(i).Rows.Count > 0) Then
                                For Each dr As DataRow In dt.Rows
                                    If (dr.Item("Owner_Product_no") & "" <> "") And (dr.Item("Owner_Product_no") & "" <> "9999999999") Then
                                        If (dr.Item("Owner_Product_no") & "" = CType(Session("MAT_USE"), DataSet).Tables(i).Rows(0).Item("Owner") & "") Then
                                            dtBody = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(i), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "C", dr.Item("Owner_Product_no")).Tables(0)
                                            If dtBody.Rows.Count > 0 Then

                                                HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
                                                HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                                                HTML &= "<tr ><td style='height:3px;' colspan='6'><hr /></td></tr>"
                                                HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                                                HTML &= "</table> "

                                                HTML &= Gen_Report(HeadReport, dr.Item("Owner_Product_name"), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))

                                            End If
                                        End If

                                    End If
                                Next
                            End If


                        Next
                        'For Each dr As DataRow In dt.Rows
                        '    If (dr.Item("Owner_Product_no") & "" <> "") And (dr.Item("Owner_Product_no") & "" <> "9999999999") Then

                        '        dtBody = GetMaterialRate(ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "C", dr.Item("Owner_Product_no")).Tables(0)
                        '        If dtBody.Rows.Count > 0 Then

                        '            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
                        '            HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                        '            HTML &= "<tr ><td style='height:3px;' colspan='6'><hr /></td></tr>"
                        '            HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                        '            HTML &= "</table> "

                        '            HTML &= Gen_Report(HeadReport, dr.Item("Owner_Product_name"), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                        '        End If

                        '    End If
                        'Next
                    End If

                    '************************************************************
                    '****** ค่าประมาณการใบจริง **************************************
                    '************************************************************
                Else
                    btnConfirm.Width = 160
                    btnConfirm.Text = "ยืนยันค่าประมาณการ"
                    lbEstimate.Text = "ESTIMATE"
                    lbEstimate.Visible = True
                    HeadReport = dsConfig.Tables(0).Rows(0).Item("Head_Report_Compute")
                    'ตัวจริง
                    Dim ds As New DataSet
                    If (Session("ViewestimeteOld") Is Nothing) Then
                        If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                            'Liquid
                            Dim dtBody As DataTable = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "A", ViewState("VEHICLE_DETAIL").ToString().Split("$")(14)).Tables(0)
                            ds.Tables.Add(dtBody.Copy)
                            ds.AcceptChanges()
                            HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                            ' ViewState("CodeMatFree") รหัสของ MAT ที่ฟรี
                        Else
                            'Bulk
                            Dim CodeMatFree As String = ""
                            Dim dtBody As DataTable = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "A", ViewState("VEHICLE_DETAIL").ToString().Split("$")(14)).Tables(0)
                            ds.Tables.Add(dtBody.Copy)
                            ds.AcceptChanges()
                            CodeMatFree = ViewState("CodeMatFree")
                            Dim Customer_no As String = "$"
                            HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                            Dim counttable As Integer = 1

                            For i As Integer = 1 To CType(Session("MAT_USE"), DataSet).Tables.Count - 1
                                If (CType(Session("MAT_USE"), DataSet).Tables(i).Rows.Count > 0) Then
                                    For Each dr As DataRow In dt.Rows
                                        If (dr.Item("Owner_Product_no") & "" <> "") And (dr.Item("Owner_Product_no") & "" <> "9999999999") Then
                                            If (dr.Item("Owner_Product_no") & "" = CType(Session("MAT_USE"), DataSet).Tables(i).Rows(0).Item("Owner") & "") Then
                                                dtBody = GetMaterialRate(CType(Session("MAT_USE"), DataSet).Tables(i), ViewState("VEHICLE_DETAIL").ToString().Split("$")(8), ViewState("VEHICLE_DETAIL").ToString().Split("$")(9), ViewState("VEHICLE_DETAIL").ToString().Split("$")(10), BuyerSeller, "C", dr.Item("Owner_Product_no")).Tables(0)
                                                If dtBody.Rows.Count > 0 Then
                                                    dtBody.TableName = "tableEs" & counttable
                                                    counttable += 1
                                                    ds.Tables.Add(dtBody.Copy)
                                                    CodeMatFree &= "," & ViewState("CodeMatFree")
                                                    Customer_no &= dr.Item("Owner_Product_no") & "$"   'รหัสของเจ้าขอสินค้า
                                                    HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
                                                    HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                                                    HTML &= "<tr ><td style='height:3px;' colspan='6'><hr /></td></tr>"
                                                    HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                                                    HTML &= "</table> "

                                                    HTML &= Gen_Report(HeadReport, dr.Item("Owner_Product_name"), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))

                                                End If
                                            End If

                                        End If
                                    Next
                                End If
                            Next
                            If (Customer_no & "" <> "") Then
                                Session("Customer_SAP") = Customer_no
                            End If
                        End If
                    Else
                        'กรณี View ใบประมาณการเก่า
                        lbEstimate.Text = "ESTIMATE"
                        lbEstimate.Visible = True
                        If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                            'Liquid
                            Dim dtBody As DataTable = clsGet.GetTemp_Estimate(Session("ViewestimeteOld"), "")
                            ds.Tables.Add(dtBody.Copy)
                            ds.AcceptChanges()
                            HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                            ' ViewState("CodeMatFree") รหัสของ MAT ที่ฟรี
                        Else
                            'Bulk
                            Dim dtBody As DataTable = clsGet.GetTemp_Estimate(Session("ViewestimeteOld"), Session("Owner_OLD_ESTIMATE") & "")
                            ds.Tables.Add(dtBody.Copy)
                            ds.AcceptChanges()
                            HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                            '    Dim CodeMatFree As String = ""
                            '    Dim dtOwner As DataTable = clsGet.GetTemp_EstimateOwnerForOldEstimate(Session("ViewestimeteOld"))

                            '    If (dtOwner.Rows.Count > 0) Then
                            '        Dim dtBody As DataTable = clsGet.GetTemp_Estimate(Session("ViewestimeteOld"), dtOwner.Rows(0).Item("Owner_Product_no"))
                            '        ds.Tables.Add(dtBody.Copy)
                            '        ds.AcceptChanges()
                            '        CodeMatFree = ViewState("CodeMatFree")
                            '        Dim Customer_no As String = dtBody.Rows(0).Item("Owner_Product")
                            '        HTML = Gen_Report(HeadReport, ViewState("VEHICLE_DETAIL").ToString().Split("$")(0), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                            '        Dim counttable As Integer = 1

                            '        For Each dr As DataRow In dtOwner.Rows
                            '            If (dr.Item("Owner_Product_no") & "" <> "") Then
                            '                If (dr.Item("Owner_Product_no") <> Customer_no) Then
                            '                    dtBody = clsGet.GetTemp_Estimate(Session("ViewestimeteOld"), dr.Item("Owner_Product_no"))
                            '                    'Customer_no = dr.Item("Owner_Product")
                            '                    dtBody.TableName = "tableEs" & counttable
                            '                    counttable += 1
                            '                    ds.Tables.Add(dtBody.Copy)
                            '                    ds.AcceptChanges()
                            '                    CodeMatFree &= "," & ViewState("CodeMatFree")
                            '                    Customer_no &= dr.Item("Owner_Product_no") & "$"   'รหัสของเจ้าขอสินค้า
                            '                    HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
                            '                    HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                            '                    HTML &= "<tr ><td style='height:3px;' colspan='6'><hr /></td></tr>"
                            '                    HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
                            '                    HTML &= "</table> "

                            '                    HTML &= Gen_Report(HeadReport, dr.Item("Owner_Product_name"), ViewState("VEHICLE_DETAIL").ToString().Split("$")(1), ViewState("VEHICLE_DETAIL").ToString().Split("$")(2), ViewState("VEHICLE_DETAIL").ToString().Split("$")(3), ViewState("VEHICLE_DETAIL").ToString().Split("$")(4), ViewState("VEHICLE_DETAIL").ToString().Split("$")(5), ViewState("VEHICLE_DETAIL").ToString().Split("$")(6), Gen_Body(Vat, dtBody), ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))

                            '                End If
                            '            End If
                            '        Next
                            '    End If

                        End If

                    End If

                    If (Session("Booking_no_ESTIMATE") & "" <> "") Then
                        Session("dsMat_SAP") = ds
                        Session("CodeMATFree_SAP") = ViewState("CodeMatFree")
                    End If

                End If
                LiEstimate.Text = HTML
            End If

        End If
    End Sub

    Private Function Gen_Report(ByVal HeadReport As String, ByVal SoldTo As String, ByVal Shipname As String, ByVal Product As String, ByVal DWT As String, ByVal LOA As String, ByVal TotalProduct As String, ByVal DF As String, ByVal Body As String, ByVal GRT As String) As String
        Dim HTML As String = ""
        Dim Simulate As String = Request.QueryString("Simulate")
        If (Simulate & "" = "Y") Then
            btnConfirm.Visible = False
            btnPrint.Visible = True
            btnClose.Visible = True
            'HeadReport = "Simulate Booking"
            lbEstimate.Text = "SIMULATE BOOKING"
        End If
        lbShip.Text = "SHIP NAME : " & Shipname
        lbAgent.Text = "SHIP AGENT : " & SoldTo
        lbLoA.Text = "L.O.A.(ft) : " & FormatNumber(LOA, 2)
        lbGrt.Text = "GRT : " & FormatNumber(GRT, 2)
        lbdWT.Text = "DWT : " & FormatNumber(DWT, 2)
        lbPo.Text = "PO : " & FormatNumber(TotalProduct, 3) & " MT"
        lbProductName.Text = "PRODUCT : " & Product
        HTML = "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr ><td colspan='6'>" & Body & "</td></tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
        Return HTML
    End Function

    Private Sub FormatN(ByRef data As String)
        Try
            data = FormatNumber(data, 2)
        Catch ex As Exception

        End Try
    End Sub

    Private Function Gen_Body(ByVal Vat As String, ByVal DSItem As DataTable) As String
        Dim HTML As String = ""
        HTML = "<table width='100%'  style='border-width: 0px;' border='1' cellpadding='0' cellspacing='0'>"
        HTML &= "<tr style='background-color:#32cd32;'>"
        HTML &= "<td style='width:140px;' valign='top'  align='center'><font class='White-Style-Head' style='color:#000000'>Code</font>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='center'><font class='White-Style-Head' style='color:#000000'>DESCRIPTION</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>RATE/UNIT</font></td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td align='right'  valign='center'><font class='White-Style-Head' style='color:#000000'>AMOUNT</font></td>"
        HTML &= "</tr>"
        Dim Value As Double = 0
        Dim ValueVat As Double = 0
        Dim WHT As Double = 0
        Dim row As Integer = 2
        ViewState("DSItem") = DSItem
        For Each dr As DataRow In DSItem.Rows
            If (CDbl(dr.Item("data4")) > 0.0) Then
                'Dim MatName As String = dr.Item("MAT_NAME") & "<BR /> - " & dr.Item("name")
                'If (dr.Item("MAT_NAME") = dr.Item("name")) Then
                '    MatName = dr.Item("MAT_NAME")
                'End If
                Dim MatName As String = dr.Item("MAT_NAME")
                If (row Mod 2) Then
                    HTML &= "<tr>"
                Else
                    HTML &= "<tr style='background-color:#F0F9FB;'>"
                End If
                row += 1
                Dim data1 As String = dr.Item("data1")
                Dim data2 As String = dr.Item("data2")
                Dim data3 As String = dr.Item("data3")
                Dim data4 As String = dr.Item("data4")
                FormatN(data1)
                FormatN(data2)
                ' FormatN(data3)
                FormatN(data4)
                Dim Mat_Code As String = ""
                Try
                    Mat_Code = CInt(dr.Item("id"))
                Catch ex As Exception
                    Mat_Code = dr.Item("id")
                End Try
                HTML &= "<td style='width:140px;' valign='top'  align='left'><font class='White-Style' style='color:#000000'>" & Mat_Code & "</font>&nbsp;</td>"
                HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>" & MatName & "</font></td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data1 & "</font>&nbsp;</td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data2 & "</font></td>"
                HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style' style='color:#000000'>" & data3 & "</font>&nbsp;</td>"
                HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & data4 & "</font></td>"
                HTML &= "</tr>"
                Value = Value + CDbl(dr.Item("data4"))
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(dr.Item("id"))
                If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                    '1%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                    '3%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                    '5%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                End If
            End If

        Next


        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 1px 0px 0px 0px;' colspan='5' align='left'>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        'HTML &= "</tr>"


        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE       (EXCLUSIVE OF VAT)</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        HTML &= "</tr>"

        ValueVat = ((Value * CDbl(Vat)) / 100).ToString("############.00")
        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>Vat " & Vat & "%</font>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat) & "</font></td>"
        'HTML &= "</tr>"
        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='right'><font class='White-Style' style='color:#000000'>ADD VAT " & Vat & "%   </font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat, 2) & "</font></td>"
        HTML &= "</tr>"


        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>รวมประเมินค่าใช้จ่าย</font>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00")) & "</font></td>"
        'HTML &= "</tr>"
        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE       (INCLUSIVE OF VAT)</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00"), 2) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "</table>"

        HTML &= Gen_Sum(Value.ToString("#############0.00"), ValueVat.ToString(), WHT.ToString())
        Return HTML
    End Function

    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        dt.Columns.Add("id", GetType(String))
        dt.Columns.Add("MAT_NAME", GetType(String))
        dt.Columns.Add("name", GetType(String))
        dt.Columns.Add("data1", GetType(String))
        dt.Columns.Add("data2", GetType(String))
        dt.Columns.Add("data3", GetType(String))
        dt.Columns.Add("data4", GetType(String))
        ds.Tables(0).AcceptChanges()
        ds.AcceptChanges()
        Return ds
    End Function

    Private Function GetMaterialRate(ByVal dt As DataTable, ByVal Destinatin As String, ByVal TypeBuy As String, ByVal VesselNational As String, ByVal Buyerseller As String, ByVal Mat_For As String, ByVal Customer_no As String) As DataSet
        ' Dim dt As DataTable = CType(Session("MAT_USE"), DataSet).Tables(0)
        Dim ds As DataSet = CreateDataset()
        Dim CodeMatFree As String = "''"
        Dim Shifting As Boolean = False
        Try
            Dim dtWharf As DataTable = clsGet.Get_BookingWharf(Session("Booking_no_ESTIMATE"))
            If (dtWharf.Rows.Count > 0) Then
                If (dtWharf.Rows(0).Item("Shift_Booking") & "" <> "") Then
                    Shifting = True
                End If
            End If
        Catch ex As Exception

        End Try

        For Each dr As DataRow In dt.Rows
            If True Then
                Dim dtRelation As DataTable = clsGet.Get_MeterailRelation(dr.Item("MATERIAL"))
                Dim dtMat As DataTable = clsGet.Get_MeterailAllField(dr.Item("MATERIAL"))
                If (True) Then
                    'If (dtMat.Rows(0).Item("MAT_FOR") & "" = Mat_For) Then
                    Dim dtProduct As DataTable = Session("Product_popup")
                    Dim Owner As String = ViewState("VEHICLE_DETAIL").ToString().Split("-")(0)
                    Dim dtBooking As DataTable
                    Try
                        dtBooking = clsGet.Get_BookingOneRecord(Session("Booking_no_ESTIMATE")).Tables(0)
                    Catch ex As Exception

                    End Try

                    Dim GRT As Double = CDbl(ViewState("VEHICLE_DETAIL").ToString().Split("$")(11).Replace("MT", ""))
                    Dim LOA As Double = CDbl(ViewState("VEHICLE_DETAIL").ToString().Split("$")(4).Replace("MT", ""))
                    Dim DWT As Double = CDbl(ViewState("VEHICLE_DETAIL").ToString().Split("$")(3).Replace("MT", ""))
                    Dim Draft As Double = CDbl(ViewState("VEHICLE_DETAIL").ToString().Split("$")(6).Replace("MT", ""))
                    Dim ProductName As String = ViewState("VEHICLE_DETAIL").ToString().Split("$")(13)
                    Dim ProductValue As Double = CDbl(ViewState("VEHICLE_DETAIL").ToString().Split("$")(5).Replace("MT", ""))
                    If Not (dtBooking Is Nothing) Then
                        If (dtBooking.Rows(0).Item("PORT") & "" = "B") Then
                            If Not (dtBooking.Rows(0).Item("MAgent_no") = Owner) Then
                                ProductName = ""
                                ProductValue = 0
                                Try
                                    Dim dtTemp As DataTable = dtProduct.Clone()
                                    For Each drrr As DataRow In dtProduct.Rows
                                        If (drrr.Item("Owner_Product_no") = Owner) Then
                                            dtTemp.ImportRow(drrr)
                                            dtTemp.AcceptChanges()
                                            If ProductName = "" Then
                                                ProductName = drrr.Item("Product_name") & "฿" & drrr.Item("Product_Amount")
                                            Else
                                                ProductName &= "," & drrr.Item("Product_name") & "฿" & drrr.Item("Product_Amount")
                                            End If
                                            ProductValue += CDbl(drrr.Item("Product_Amount"))
                                        End If
                                    Next
                                    dtProduct = dtTemp
                                Catch ex As Exception

                                End Try

                            End If
                        End If

                    End If


                    'ทำการเช็คว่าคิดเงินหรือไม่
                    If ((CheckFreeMaterial(dtMat, dtProduct, Destinatin, TypeBuy, VesselNational, Buyerseller, DWT)) Or (dr.Item("MAT_ComputeM") & "" = "Y")) Then
                        'พบข้อมูลการผูกสูตร
                        If (dtRelation.Rows.Count > 0) Then
                            Dim Value As Double = 0.0
                            Select Case dtRelation.Rows(0).Item("Relation")
                                Case "01" 'สูตรคำนวนน้ำหนักเรือ GRT 
                                    Value = GRT
                                Case "02" 'L.O.A.
                                    Value = LOA
                                Case "03" 'DWT
                                    Value = DWT
                                Case "04" 'Draft
                                    Value = Draft
                                Case "05" 'Product
                                    Value = ProductValue
                                Case "06"
                                    Value = 0.0
                            End Select

                            Dim conditioncheck As Boolean = False
                            For Each drR As DataRow In dtRelation.Rows
                                'ทำการเช็คสัญชาติเรือและทำการเช็คเรือขนส่งในประเทศหรือนอกประเทศ
                                If (drR.Item("Vessel_national") = "A") Then
                                    If (drR.Item("Vessel_Destination") = "A") Then
                                        conditioncheck = True
                                    ElseIf (drR.Item("Vessel_Destination") = "I") Then
                                        If (Destinatin = "I") Then
                                            conditioncheck = True
                                        Else
                                            conditioncheck = False
                                        End If
                                    ElseIf (drR.Item("Vessel_Destination") = "O") Then
                                        If (Destinatin = "0") Then
                                            conditioncheck = True
                                        Else
                                            conditioncheck = False
                                        End If
                                    End If
                                ElseIf (drR.Item("Vessel_national") = "T") Then
                                    If (VesselNational = "T") Then
                                        If (drR.Item("Vessel_Destination") = "A") Then
                                            conditioncheck = True
                                        ElseIf (drR.Item("Vessel_Destination") = "I") Then
                                            If (Destinatin = "I") Then
                                                conditioncheck = True
                                            Else
                                                conditioncheck = False
                                            End If
                                        ElseIf (drR.Item("Vessel_Destination") = "O") Then
                                            If (Destinatin = "0") Then
                                                conditioncheck = True
                                            Else
                                                conditioncheck = False
                                            End If
                                        End If
                                    Else
                                        conditioncheck = False
                                    End If
                                ElseIf (drR.Item("Vessel_national") = "O") Then
                                    If (VesselNational = "O") Then
                                        If (drR.Item("Vessel_Destination") = "A") Then
                                            conditioncheck = True
                                        ElseIf (drR.Item("Vessel_Destination") = "I") Then
                                            If (Destinatin = "I") Then
                                                conditioncheck = True
                                            Else
                                                conditioncheck = False
                                            End If
                                        ElseIf (drR.Item("Vessel_Destination") = "O") Then
                                            If (Destinatin = "0") Then
                                                conditioncheck = True
                                            Else
                                                conditioncheck = False
                                            End If
                                        End If
                                    Else
                                        conditioncheck = False
                                    End If
                                End If
                                If (conditioncheck) Then
                                    'ทำการเปรียบเทียบช่วงน่ำหนักเรือ
                                    If (drR.Item("Compute_Between") & "" = "A") Then
                                        'แบบแรกตกช่วงคิด
                                        If ((CDbl(drR("Point_Start")) <= Value) And (CDbl(drR("Point_End")) >= Value)) Then
                                            'Rate ตกในช่วง
                                            Dim rate As String = drR.Item("Mat_Rate")
                                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                                For i As Integer = 0 To Cus.Length - 1
                                                    If (Cus(i).ToString = Customer_no) Then
                                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                                        Exit For
                                                    End If
                                                Next
                                            End If
                                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                                            Try
                                                drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                                            Catch ex As Exception
                                                drNew.Item("id") = drR.Item("Meterial")
                                            End Try

                                            drNew.Item("name") = drR.Item("Mat_Name")
                                            drNew.Item("MAT_NAME") = dr.Item("Text_line")
                                            If (CInt(Value).ToString() = "0") Then
                                                Value = 1.0
                                                drNew.Item("data1") = ""
                                            Else
                                                drNew.Item("data1") = Value
                                            End If
                                            Try
                                                drNew.Item("data2") = rate
                                                Dim newValue As Double = 0
                                                If (drR.Item("Point_Unit") = "0") Then
                                                    newValue = 1
                                                Else
                                                    If (drNew.Item("id") = "2100000268") Then
                                                        newValue = (Value / CDbl(drR.Item("Point_Unit"))).ToString("###################0.00")
                                                    Else
                                                        newValue = (Value / CDbl(drR.Item("Point_Unit"))).ToString("###################0.000")
                                                    End If
                                                End If
                                                Dim StardardTime As Integer = 0
                                                If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                                    Dim dsProduct As New DataTable
                                                    Dim Product() As String = ProductName.Split("฿")
                                                    Dim Before As String = ""
                                                    Dim After As String = ""
                                                    Dim PlusTank As String = ""
                                                    Dim chkBefore As Boolean = True
                                                    Dim C_Product As Integer = 0
                                                    Dim C_Parcel As Integer = 0
                                                    '00036 LPG & BUTANE (C4)
                                                    Dim LPG As Boolean = False
                                                    Dim BUTANE As Boolean = False
                                                    Dim LPG_BUTANE As Boolean = False
                                                    If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                        For i As Integer = 0 To Product.Length - 1
                                                            If (Product(i).ToString() <> "") Then
                                                                If (i Mod 2) = 0 Then
                                                                    If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                                        'Liquid
                                                                        Dim Load As String = ""
                                                                        If (dtProduct.Rows.Count > 0) Then
                                                                            Load = dtProduct.Rows(0).Item("Product_Event") & ""
                                                                        End If
                                                                        If (Load <> "") Then
                                                                            If (Load = "L") Then
                                                                                dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "L", "")
                                                                            ElseIf (Load = "D") Then
                                                                                dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "D", "")
                                                                            End If
                                                                            Try
                                                                                If (dsProduct.Rows.Count = 0) Then
                                                                                    dsProduct = clsGet.Get_StardardTime("", Load, dtProduct.Rows(0).Item("Group_no") & "")
                                                                                End If
                                                                            Catch ex As Exception

                                                                            End Try

                                                                        End If
                                                                        If (dsProduct.Rows.Count > 0) Then
                                                                            If (dsProduct.Rows(0).Item("Group_Code") = "00031") Or (dsProduct.Rows(0).Item("Group_Code") = "00064") Then
                                                                                LPG = True
                                                                            End If
                                                                            If (dsProduct.Rows(0).Item("Group_Code") = "00034") Then
                                                                                BUTANE = True
                                                                            End If
                                                                        End If

                                                                    End If
                                                                End If
                                                            End If
                                                        Next
                                                        If (LPG = True) And (BUTANE = True) Then
                                                            LPG_BUTANE = True
                                                        End If
                                                    End If
                                                    For i As Integer = 0 To Product.Length - 1
                                                        If (Product(i).ToString() <> "") Then
                                                            If (i Mod 2) = 0 Then
                                                                If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                                    'Liquid
                                                                    Dim Load As String = ""
                                                                    If (dtProduct.Rows.Count > 0) Then
                                                                        Load = dtProduct.Rows(0).Item("Product_Event") & ""
                                                                    End If
                                                                    If (Load <> "") Then
                                                                        If (Load = "L") Then
                                                                            dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "L", "")
                                                                        ElseIf (Load = "D") Then
                                                                            dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "D", "")
                                                                        End If
                                                                        Try
                                                                            If (dsProduct.Rows.Count = 0) Then
                                                                                dsProduct = clsGet.Get_StardardTime("", Load, dtProduct.Rows(0).Item("Group_no") & "")
                                                                            End If
                                                                        Catch ex As Exception

                                                                        End Try
                                                                        If (dsProduct.Rows.Count > 0) Then
                                                                            If (dsProduct.Rows(0).Item("Rate") & "" <> "") Then
                                                                                'StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))

                                                                                If (chkBefore) Then
                                                                                    chkBefore = False
                                                                                    If (LPG_BUTANE) Then
                                                                                        If (dsProduct.Rows(0).Item("Group_Code") = "00031") Or (dsProduct.Rows(0).Item("Group_Code") = "00064") Then
                                                                                            If (LPG) Then
                                                                                                If (Load = "L") Then
                                                                                                    dsProduct = clsGet.Get_StardardTime("''", "L", "00033")
                                                                                                ElseIf (Load = "D") Then
                                                                                                    dsProduct = clsGet.Get_StardardTime("''", "D", "00033")
                                                                                                End If
                                                                                                LPG = False
                                                                                            End If
                                                                                        ElseIf (dsProduct.Rows(0).Item("Group_Code") = "00034") Then
                                                                                            If (BUTANE) Then
                                                                                                If (Load = "L") Then
                                                                                                    dsProduct = clsGet.Get_StardardTime("''", "L", "00033")
                                                                                                ElseIf (Load = "D") Then
                                                                                                    dsProduct = clsGet.Get_StardardTime("''", "D", "00033")
                                                                                                End If
                                                                                                BUTANE = False
                                                                                            End If
                                                                                        End If
                                                                                    End If
                                                                                    Before = dsProduct.Rows(0).Item("Before") & ""
                                                                                    PlusTank = dsProduct.Rows(0).Item("Plus_Tank") & ""
                                                                                    Dim _Product As String = ""
                                                                                    Dim _Group As String = dsProduct.Rows(0).Item("Group_Code")
                                                                                    If (dsProduct.Rows(0).Item("Plus_Product_Before") & "" <> "00:00") Or (dsProduct.Rows(0).Item("Plus_Product_After") & "" <> "00:00") Then
                                                                                        For x As Integer = 0 To Product.Length - 1
                                                                                            Dim pdTemp As DataTable
                                                                                            If (Load & "" = "L") Then
                                                                                                pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "L", "")
                                                                                            Else
                                                                                                pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "D", "")
                                                                                            End If
                                                                                            If (pdTemp.Rows.Count > 0) Then
                                                                                                If (_Group = pdTemp.Rows(0).Item("Group_Code")) Then
                                                                                                    C_Parcel += 1
                                                                                                    ' Dim Find As Integer = InStr(_Product, Product(x).ToString(), CompareMethod.Text)
                                                                                                    'If (Find = 0) Then
                                                                                                    'C_Product += 1
                                                                                                    'End If
                                                                                                    If (_Product <> Product(x).ToString()) Then
                                                                                                        C_Product += 1
                                                                                                    End If
                                                                                                End If
                                                                                            End If
                                                                                            '  _Product &= Product(x).ToString() & ","
                                                                                            _Product = Product(x).ToString()
                                                                                        Next
                                                                                    ElseIf (dsProduct.Rows(0).Item("Plus_Parcel_Before") & "" <> "00:00") Or (dsProduct.Rows(0).Item("Plus_Parcel_After") & "" <> "00:00") Then
                                                                                        'Parcel
                                                                                        For x As Integer = 0 To Product.Length - 1
                                                                                            Dim pdTemp As DataTable
                                                                                            If (Load & "" = "L") Then
                                                                                                pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "L", "")
                                                                                            Else
                                                                                                pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "D", "")
                                                                                            End If
                                                                                            If (pdTemp.Rows.Count > 0) Then
                                                                                                If (_Group = pdTemp.Rows(0).Item("Group_Code")) Then
                                                                                                    C_Parcel += 1
                                                                                                End If
                                                                                            End If
                                                                                        Next
                                                                                    End If
                                                                                    If (C_Product > 1) Then
                                                                                        Dim PlusTime As String = "00:00"
                                                                                        For n = 1 To C_Product - 1
                                                                                            PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_Before"))
                                                                                        Next
                                                                                        Before = PlusHourMinute(Before, PlusTime)
                                                                                    ElseIf (C_Parcel > 1) Then
                                                                                        Dim PlusTime As String = "00:00"
                                                                                        For n = 1 To C_Parcel - 1
                                                                                            PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Parcel_Before"))
                                                                                        Next
                                                                                        Before = PlusHourMinute(Before, PlusTime)
                                                                                    End If
                                                                                End If
                                                                                After = dsProduct.Rows(0).Item("After") & ""
                                                                                If (C_Product > 1) Then
                                                                                    Dim PlusTime As String = "00:00"
                                                                                    For n = 1 To C_Product - 1
                                                                                        PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_After"))
                                                                                    Next
                                                                                    After = PlusHourMinute(After, PlusTime)
                                                                                ElseIf (C_Parcel > 1) Then
                                                                                    Dim PlusTime As String = "00:00"
                                                                                    For n = 1 To C_Parcel - 1
                                                                                        PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_After"))
                                                                                    Next
                                                                                    After = PlusHourMinute(After, PlusTime)
                                                                                End If
                                                                                StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                                            Else
                                                                                StardardTime = 0
                                                                                After = "00:00"
                                                                                Before = "00:00"
                                                                            End If
                                                                        End If

                                                                    End If

                                                                Else
                                                                    'Bulk
                                                                    dsProduct = clsGet.Get_StardardTimeBulk("'" & Product(i).ToString() & "'", "E")

                                                                    If (dsProduct.Rows(0).Item("StandardTime") & "" <> "") Then
                                                                        StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                                                                        'StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                                    End If
                                                                End If

                                                            End If

                                                        End If
                                                    Next
                                                    If (Before <> "") Then
                                                        Try
                                                            StardardTime = StardardTime + CInt(Before.Split(":")(0)) '+ CInt(Before.Split(":")(1))
                                                        Catch ex As Exception

                                                        End Try
                                                    End If
                                                    If (PlusTank <> "") Then
                                                        Try
                                                            StardardTime = StardardTime + CInt(PlusTank)
                                                        Catch ex As Exception

                                                        End Try
                                                    End If
                                                    If (After <> "") Then
                                                        Try
                                                            StardardTime = StardardTime + CInt(After.Split(":")(0)) ' + CInt(After.Split(":")(1))
                                                        Catch ex As Exception

                                                        End Try
                                                    End If
                                                    StardardTime = StardardTime + CInt(Session("StandardTimePlus"))
                                                End If
                                                If (drR("Data3") = "N") Then
                                                    drNew.Item("data3") = ""
                                                    drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")
                                                ElseIf (drR("Data3") = "S") Then
                                                    'ทำการคำนวณหา Stardard Time
                                                    'If (StardardTime = 0) Then
                                                    '    StardardTime = 1
                                                    'End If
                                                    Session("Lay_Time") = StardardTime
                                                    drNew.Item("data3") = StardardTime
                                                    drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                                                Else
                                                    If (drR.Item("Compute_Count") & "" = "") Then
                                                        If (drR.Item("Compute_C") & "" = "D") Then
                                                            If (StardardTime = 0) Then
                                                                drNew.Item("data3") = 1
                                                            Else
                                                                drNew.Item("data3") = ((CDbl(StardardTime) \ 24) + 1).ToString()
                                                            End If

                                                        Else
                                                            If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                                                drNew.Item("data3") = (CDbl(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                                            Else
                                                                drNew.Item("data3") = CDbl(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                                            End If
                                                        End If
                                                    Else
                                                        drNew.Item("data3") = drR.Item("Compute_Count")
                                                    End If
                                                    If (Shifting) Then
                                                        drNew.Item("data3") = CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))
                                                    End If
                                                    drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                                                End If
                                                If (drR.Item("LowRate") & "" = "") Then
                                                    drR.Item("LowRate") = "0"
                                                End If
                                                If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                                    If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                                        'ราคาต่ำกว่า ราคาขั้นต่ำ
                                                        If (drNew.Item("data3") = "") Then
                                                            drNew.Item("data2") = drR.Item("LowRate")
                                                            drNew.Item("data4") = drR.Item("LowRate")
                                                        Else
                                                            drNew.Item("data4") = drR.Item("LowRate")
                                                            drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                                        End If
                                                    End If
                                                End If
                                            Catch ex As Exception
                                                '((LOA-5)*Draft)+10%
                                                If (drR.Item("LowRate") & "" = "") Then
                                                    drR.Item("LowRate") = "0"
                                                End If
                                                Dim LoACompute As Double = ((LOA - 5.0) * Draft) + ((((LOA - 5.0) * Draft) * 10) / 100).ToString("##################0.00")
                                                If (CDbl(drR.Item("LowRate")) > LoACompute) Then
                                                    drNew.Item("data2") = drR.Item("LowRate")
                                                    drNew.Item("data3") = "1"
                                                    drNew.Item("data4") = drR.Item("LowRate")
                                                Else
                                                    drNew.Item("data2") = LoACompute.ToString()
                                                    drNew.Item("data3") = "1"
                                                    drNew.Item("data4") = LoACompute.ToString()
                                                End If
                                                If Shifting Then
                                                    If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "0") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                        drNew.Item("data3") = (CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                                        drNew.Item("data4") = (CDbl(drNew.Item("data4")) + (CDbl(drNew.Item("data4")) * CDbl(dtMat.Rows(0).Item("MAT_Shifting")))).ToString("#######################0.00")
                                                    End If
                                                End If
                                            End Try
                                            ds.Tables(0).Rows.Add(drNew)
                                            ds.Tables(0).AcceptChanges()
                                            Exit For
                                        End If
                                    Else
                                        'แบบสองผ่านช่วงไหนคิดหมด
                                        If ((CDbl(drR("Point_Start")) <= Value)) Then
                                            'Rate ตกในช่วง
                                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                                            Try
                                                drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                                            Catch ex As Exception
                                                drNew.Item("id") = drR.Item("Meterial")
                                            End Try

                                            drNew.Item("name") = drR.Item("Mat_Name")
                                            drNew.Item("MAT_NAME") = dr.Item("Text_line")
                                            Dim Nvalue As Double = 0
                                            If (Value > drR("Point_End")) Then
                                                Nvalue = drR("Point_End") - CDbl(drR("Point_Start"))
                                            Else
                                                Nvalue = Value - CDbl(drR("Point_Start"))
                                            End If
                                            Dim rate As String = drR.Item("Mat_Rate")
                                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                                For i As Integer = 0 To Cus.Length - 1
                                                    If (Cus(i).ToString = Customer_no) Then
                                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                                        Exit For
                                                    End If
                                                Next
                                            End If
                                            drNew.Item("data1") = Nvalue
                                            drNew.Item("data2") = rate

                                            Dim newValue As Double = 0
                                            If (drR.Item("Point_Unit") = "0") Then
                                                newValue = 1
                                            Else
                                                newValue = (Nvalue / CDbl(drR.Item("Point_Unit")))
                                            End If
                                            Dim StardardTime As Integer = 0
                                            If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                                Dim dsProduct As New DataTable
                                                Dim Product() As String = ProductName.Split("฿")
                                                Dim Before As String = ""
                                                Dim After As String = ""
                                                Dim PlusTank As String = ""
                                                Dim chkBefore As Boolean = True
                                                Dim C_Product As Integer = 0
                                                Dim C_Parcel As Integer = 0
                                                '00036 LPG & BUTANE (C4)
                                                Dim LPG As Boolean = False
                                                Dim BUTANE As Boolean = False
                                                Dim LPG_BUTANE As Boolean = False
                                                If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                    For i As Integer = 0 To Product.Length - 1
                                                        If (Product(i).ToString() <> "") Then
                                                            If (i Mod 2) = 0 Then
                                                                If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                                    'Liquid
                                                                    Dim Load As String = ""
                                                                    If (dtProduct.Rows.Count > 0) Then
                                                                        Load = dtProduct.Rows(0).Item("Product_Event") & ""
                                                                    End If
                                                                    If (Load <> "") Then
                                                                        If (Load = "L") Then
                                                                            dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "L", "")
                                                                        ElseIf (Load = "D") Then
                                                                            dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "D", "")
                                                                        End If
                                                                    End If
                                                                    If (dsProduct.Rows(0).Item("Group_Code") = "00031") Or (dsProduct.Rows(0).Item("Group_Code") = "00064") Then
                                                                        LPG = True
                                                                    End If
                                                                    If (dsProduct.Rows(0).Item("Group_Code") = "00034") Then
                                                                        BUTANE = True
                                                                    End If
                                                                End If
                                                            End If
                                                        End If
                                                    Next
                                                    If (LPG = True) And (BUTANE = True) Then
                                                        LPG_BUTANE = True
                                                    End If
                                                End If
                                                For i As Integer = 0 To Product.Length - 1
                                                    If (Product(i).ToString() <> "") Then
                                                        If (i Mod 2) = 0 Then
                                                            If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                                'Liquid
                                                                Dim Load As String = ""
                                                                If (dtProduct.Rows.Count > 0) Then
                                                                    Load = dtProduct.Rows(0).Item("Product_Event") & ""
                                                                End If
                                                                If (Load <> "") Then
                                                                    If (Load = "L") Then
                                                                        dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "L", "")
                                                                    ElseIf (Load = "D") Then
                                                                        dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "D", "")
                                                                    End If
                                                                    If (dsProduct.Rows.Count > 0) Then
                                                                        If (dsProduct.Rows(0).Item("Rate") & "" <> "") Then
                                                                            'StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))

                                                                            If (chkBefore) Then
                                                                                chkBefore = False
                                                                                If (LPG_BUTANE) Then
                                                                                    If (dsProduct.Rows(0).Item("Group_Code") = "00031") Or (dsProduct.Rows(0).Item("Group_Code") = "00064") Then
                                                                                        If (LPG) Then
                                                                                            If (Load = "L") Then
                                                                                                dsProduct = clsGet.Get_StardardTime("''", "L", "00033")
                                                                                            ElseIf (Load = "D") Then
                                                                                                dsProduct = clsGet.Get_StardardTime("''", "D", "00033")
                                                                                            End If
                                                                                            LPG = False
                                                                                        End If
                                                                                    ElseIf (dsProduct.Rows(0).Item("Group_Code") = "00034") Then
                                                                                        If (BUTANE) Then
                                                                                            If (Load = "L") Then
                                                                                                dsProduct = clsGet.Get_StardardTime("''", "L", "00033")
                                                                                            ElseIf (Load = "D") Then
                                                                                                dsProduct = clsGet.Get_StardardTime("''", "D", "00033")
                                                                                            End If
                                                                                            BUTANE = False
                                                                                        End If
                                                                                    End If
                                                                                End If
                                                                                Before = dsProduct.Rows(0).Item("Before") & ""
                                                                                PlusTank = dsProduct.Rows(0).Item("Plus_Tank") & ""
                                                                                Dim _Product As String = ""
                                                                                Dim _Group As String = dsProduct.Rows(0).Item("Group_Code")
                                                                                If (dsProduct.Rows(0).Item("Plus_Product_Before") & "" <> "00:00") Or (dsProduct.Rows(0).Item("Plus_Product_After") & "" <> "00:00") Then
                                                                                    For x As Integer = 0 To Product.Length - 1
                                                                                        Dim pdTemp As DataTable
                                                                                        If (Load & "" = "L") Then
                                                                                            pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "L", "")
                                                                                        Else
                                                                                            pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "D", "")
                                                                                        End If
                                                                                        If (pdTemp.Rows.Count > 0) Then
                                                                                            If (_Group = pdTemp.Rows(0).Item("Group_Code")) Then
                                                                                                C_Parcel += 1
                                                                                                ' Dim Find As Integer = InStr(_Product, Product(x).ToString(), CompareMethod.Text)
                                                                                                'If (Find = 0) Then
                                                                                                'C_Product += 1
                                                                                                'End If
                                                                                                If (_Product <> Product(x).ToString()) Then
                                                                                                    C_Product += 1
                                                                                                End If
                                                                                            End If
                                                                                        End If
                                                                                        '  _Product &= Product(x).ToString() & ","
                                                                                        _Product = Product(x).ToString()
                                                                                    Next
                                                                                ElseIf (dsProduct.Rows(0).Item("Plus_Parcel_Before") & "" <> "00:00") Or (dsProduct.Rows(0).Item("Plus_Parcel_After") & "" <> "00:00") Then
                                                                                    'Parcel
                                                                                    For x As Integer = 0 To Product.Length - 1
                                                                                        Dim pdTemp As DataTable
                                                                                        If (Load & "" = "L") Then
                                                                                            pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "L", "")
                                                                                        Else
                                                                                            pdTemp = clsGet.Get_StardardTime("'" & Product(x).ToString() & "'", "D", "")
                                                                                        End If
                                                                                        If (pdTemp.Rows.Count > 0) Then
                                                                                            If (_Group = pdTemp.Rows(0).Item("Group_Code")) Then
                                                                                                C_Parcel += 1
                                                                                            End If
                                                                                        End If
                                                                                    Next
                                                                                End If
                                                                                If (C_Product > 1) Then
                                                                                    Dim PlusTime As String = "00:00"
                                                                                    For n = 1 To C_Product - 1
                                                                                        PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_Before"))
                                                                                    Next
                                                                                    Before = PlusHourMinute(Before, PlusTime)
                                                                                ElseIf (C_Parcel > 1) Then
                                                                                    Dim PlusTime As String = "00:00"
                                                                                    For n = 1 To C_Parcel - 1
                                                                                        PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Parcel_Before"))
                                                                                    Next
                                                                                    Before = PlusHourMinute(Before, PlusTime)
                                                                                End If
                                                                            End If
                                                                            After = dsProduct.Rows(0).Item("After") & ""
                                                                            If (C_Product > 1) Then
                                                                                Dim PlusTime As String = "00:00"
                                                                                For n = 1 To C_Product - 1
                                                                                    PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_After"))
                                                                                Next
                                                                                After = PlusHourMinute(After, PlusTime)
                                                                            ElseIf (C_Parcel > 1) Then
                                                                                Dim PlusTime As String = "00:00"
                                                                                For n = 1 To C_Parcel - 1
                                                                                    PlusTime = PlusHourMinute(PlusTime, dsProduct.Rows(0).Item("Plus_Product_After"))
                                                                                Next
                                                                                After = PlusHourMinute(After, PlusTime)
                                                                            End If
                                                                            StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                                        Else
                                                                            StardardTime = 0
                                                                            After = "00:00"
                                                                            Before = "00:00"
                                                                        End If
                                                                    End If

                                                                End If

                                                            Else
                                                                'Bulk
                                                                dsProduct = clsGet.Get_StardardTimeBulk("'" & Product(i).ToString() & "'", "E")

                                                                If (dsProduct.Rows(0).Item("StandardTime") & "" <> "") Then
                                                                    StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                                                                    'StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                                End If
                                                            End If

                                                        End If

                                                    End If
                                                Next
                                                If (Before <> "") Then
                                                    Try
                                                        StardardTime = StardardTime + CInt(Before.Split(":")(0)) '+ CInt(Before.Split(":")(1))
                                                    Catch ex As Exception

                                                    End Try
                                                End If
                                                If (PlusTank <> "") Then
                                                    Try
                                                        StardardTime = StardardTime + CInt(PlusTank)
                                                    Catch ex As Exception

                                                    End Try
                                                End If
                                                If (After <> "") Then
                                                    Try
                                                        StardardTime = StardardTime + CInt(After.Split(":")(0)) ' + CInt(After.Split(":")(1))
                                                    Catch ex As Exception

                                                    End Try
                                                End If
                                                StardardTime = StardardTime + CInt(Session("StandardTimePlus"))
                                                'Dim dsProduct As New DataTable
                                                'Dim Product() As String = ProductName.Split("฿")
                                                'For i As Integer = 0 To Product.Length - 1
                                                '    If (Product(i).ToString() <> "") Then
                                                '        If (i Mod 2) = 0 Then
                                                '            If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                                '                'Liquid

                                                '                Dim Load As String = ""
                                                '                If (dtProduct.Rows.Count > 0) Then
                                                '                    Load = dtProduct.Rows(0).Item("Product_Event") & ""
                                                '                End If
                                                '                If (Load <> "") Then
                                                '                    If (Load = "L") Then
                                                '                        dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "L", "")
                                                '                    ElseIf (Load = "D") Then
                                                '                        dsProduct = clsGet.Get_StardardTime("'" & Product(i).ToString() & "'", "D", "")
                                                '                    End If

                                                '                    If (dsProduct.Rows(0).Item("Rate") & "" <> "") Then
                                                '                        'StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                                                '                        StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                '                    End If
                                                '                End If
                                                '            Else
                                                '                'Bulk
                                                '                dsProduct = clsGet.Get_StardardTimeBulk("'" & Product(i).ToString() & "'", "E")

                                                '                If (dsProduct.Rows(0).Item("StandardTime") & "" <> "") Then
                                                '                    StardardTime = StardardTime + (CInt(Product(i + 1)) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                                                '                    'StardardTime = StardardTime + (CInt(Product(i + 1)) / CInt(dsProduct.Rows(0).Item("Rate")))
                                                '                End If
                                                '            End If

                                                '        End If

                                                '    End If
                                                'Next
                                                'StardardTime = StardardTime + CInt(Session("StandardTimePlus"))
                                            End If
                                            If (drR("Data3") = "N") Then
                                                drNew.Item("data3") = ""
                                                If Shifting Then
                                                    If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "0") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                        drNew.Item("data3") = (CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                                        drNew.Item("data4") = ((newValue * CDbl(rate)) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                                                    End If
                                                Else
                                                    drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")
                                                End If


                                            ElseIf (drR("Data3") = "S") Then
                                                'ทำการคำนวณหา Stardard Time
                                                'If (StardardTime = 0) Then
                                                '    StardardTime = 1
                                                'End If
                                                Session("Lay_Time") = StardardTime
                                                drNew.Item("data3") = StardardTime
                                                If Shifting Then
                                                    If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "0") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                        drNew.Item("data3") = (CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                                    End If
                                                End If


                                                drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                                            Else
                                                If (drR.Item("Compute_Count") & "" = "") Then
                                                    If (drR.Item("Compute_C") & "" = "D") Then
                                                        drNew.Item("data3") = (CInt(StardardTime) \ 24).ToString()
                                                    Else
                                                        If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                                            drNew.Item("data3") = (CInt(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                                        Else
                                                            drNew.Item("data3") = CInt(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                                        End If
                                                    End If
                                                Else
                                                    drNew.Item("data3") = drR.Item("Compute_Count")
                                                End If
                                                'If Session("Shifting") & "" = "Y" Then
                                                '    If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                '        drNew.Item("data3") = (CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                                '    End If
                                                'End If
                                                If (Shifting) Then
                                                    drNew.Item("data3") = CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))
                                                End If
                                                drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                                            End If
                                            If (drR.Item("LowRate") & "" = "") Then
                                                drR.Item("LowRate") = "0"
                                            End If
                                            If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                                If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                                    'ราคาต่ำกว่า ราคาขั้นต่ำ
                                                    If (drNew.Item("data3") = "") Then
                                                        drNew.Item("data2") = drR.Item("LowRate")
                                                        drNew.Item("data4") = drR.Item("LowRate")
                                                    Else
                                                        If Shifting Then
                                                            If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "0") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                                drNew.Item("data3") = (CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                                            End If
                                                        End If

                                                        drNew.Item("data4") = drR.Item("LowRate")
                                                        drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                                    End If
                                                End If
                                            End If

                                            ds.Tables(0).Rows.Add(drNew)
                                            ds.Tables(0).AcceptChanges()
                                            Exit For
                                        End If
                                    End If
                                End If
                            Next
                        Else
                            'ไม่พบข้อมูลการผูกสูตร
                            Dim Plant As String = "1104"
                            If (ViewState("VEHICLE_DETAIL").ToString().Split("$")(12) = "L") Then
                                Plant = "1103"
                            End If
                            dtRelation = clsGet.Get_SubMeterail(dr.Item("MATERIAL"))
                            If (dtRelation.Rows.Count > 0) Then
                                For Each dm As DataRow In dtRelation.Rows
                                    If (Plant = dm.Item("WERKS")) Then
                                        Dim drNew As DataRow = ds.Tables(0).NewRow()
                                        Dim id As String = ""
                                        Try
                                            id = CInt(dr.Item("Material")).ToString()
                                        Catch ex As Exception
                                            id = dr.Item("Material")
                                        End Try
                                        drNew.Item("id") = id
                                        drNew.Item("name") = dr.Item("Text_line")
                                        drNew.Item("MAT_NAME") = dr.Item("Text_line")
                                        drNew.Item("data1") = ""
                                        drNew.Item("data2") = dm.Item("KBETR")
                                        drNew.Item("data3") = dr("Count")
                                        If (dr("Count") & "" = "") Then
                                            dr("Count") = "1"
                                        End If
                                        If Shifting Then
                                            If Not ((dtMat.Rows(0).Item("MAT_Shifting") & "" = "0") Or (dtMat.Rows(0).Item("MAT_Shifting") & "" = "")) Then
                                                drNew.Item("data3") = (CDbl(drNew.Item("data3")) + CDbl(dtMat.Rows(0).Item("MAT_Shifting"))).ToString("#################0.00")
                                            End If
                                        End If


                                        drNew.Item("data4") = CDbl(dm.Item("KBETR") * CDbl(dr("Count"))).ToString("###############0.00")
                                        ds.Tables(0).Rows.Add(drNew)
                                        ds.Tables(0).AcceptChanges()
                                    End If
                                Next


                            End If
                        End If

                    Else
                        CodeMatFree &= ",'" & dr.Item("MATERIAL") & "'"
                    End If

                End If
            Else
                CodeMatFree &= ",'" & dr.Item("MATERIAL") & "'"
            End If
        Next
        ViewState("CodeMatFree") = CodeMatFree
        Return ds
    End Function

    Private Function CheckProductGroup(ByVal Mat As DataTable, ByVal Product As DataTable) As Boolean
        If (Mat.Rows(0).Item("MAT_ComputeProduct") & "" = "A") Then
            Return True
        ElseIf (Mat.Rows(0).Item("MAT_ComputeProduct") & "" = "B") Then
            'เฉพาะ
            Dim dtP As DataTable = clsGet.Get_Mat_PRoduct(Mat.Rows(0).Item("MATERIAL"), "C")
            Dim condition As Boolean = False
            For i As Integer = 0 To dtP.Rows.Count - 1
                For j As Integer = 0 To Product.Rows.Count - 1
                    If (dtP.Rows(i).Item("Product_no") = Product.Rows(j).Item("Product_no")) Then
                        Return True
                    End If
                Next
            Next
            Return False
        ElseIf (Mat.Rows(0).Item("MAT_ComputeProduct") & "" = "C") Then
            'ยกเว้น
            Dim dtP As DataTable = clsGet.Get_Mat_PRoduct(Mat.Rows(0).Item("MATERIAL"), "C")
            Dim condition As Boolean = False
            For i As Integer = 0 To dtP.Rows.Count - 1
                For j As Integer = 0 To Product.Rows.Count - 1
                    If (dtP.Rows(i).Item("Product_no") = Product.Rows(j).Item("Product_no")) Then
                        Return False
                    End If
                Next
            Next
            Return True
        Else
            Return True
        End If
    End Function


    Private Function CheckFreeMaterial(ByVal Mat As DataTable, ByVal Product As DataTable, ByVal Destination As String, ByVal TypeBUy As String, ByVal VesselNational As String, ByVal BuyerSeller As String, ByVal DWT As String) As Boolean
        Dim DWToperate As String = "0"
        Dim Operate As String = Mat.Rows(0).Item("MAT_Operate") & ""
        Try
            DWToperate = CInt(DWT)
        Catch ex As Exception

        End Try

        If (Mat.Rows(0).Item("MAT_Compute") = "A") Then
            'คำนวนเงินเมื่อ
            Return CheckComputeMoney(Mat, Product, Operate, DWToperate, BuyerSeller, VesselNational, Destination, TypeBUy)

        ElseIf (Mat.Rows(0).Item("MAT_Compute") = "B") Then
            'ไม่คำนวณเงินเมื่อ
            Return Not (CheckComputeMoney(Mat, Product, Operate, DWToperate, BuyerSeller, VesselNational, Destination, TypeBUy))
        End If

    End Function

    Private Function CheckComputeMoney(ByVal MAT As DataTable, ByVal Product As DataTable, ByVal Operate As String, ByVal DWTOperate As String, ByVal BuyerSeller As String, ByVal VesselNational As String, ByVal Destination As String, ByVal TypeBuy As String) As Boolean
        Dim chkOperate As Boolean = False
        Dim DWTBase As Integer = 0
        Try
            DWTBase = CInt(MAT.Rows(0).Item("MAT_DWToperate"))
        Catch ex As Exception

        End Try
        If (Operate = "L") Then
            If (DWTOperate < DWTBase) Then
                chkOperate = True
            End If
        Else
            If (DWTOperate > DWTBase) Then
                chkOperate = True
            End If
        End If
        If CheckProductGroup(MAT, Product) Then
            chkOperate = True
        Else
            chkOperate = False
        End If
        If chkOperate Then
            If (MAT.Rows(0).Item("MAT_ComputebuyerSeller") & "" = "A") Then
                'เงินทุกคน
                If (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "A") Then
                    If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                        Return True
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                        If (Destination = "I") Then
                            Return True
                        Else
                            Return False
                        End If
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                        If (Destination = "O") Then
                            If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                'ทั้งหมด
                                Return True
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                'อื่นๆที่ไม่ใช่ FOB
                                If (TypeBUy = "O") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            Else
                                'FOB
                                If (TypeBUy = "F") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            End If
                        Else
                            Return False
                        End If
                    End If
                ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "T") Then
                    'Thai
                    If (VesselNational = "T") Then
                        If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                            Return True
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                            If (Destination = "I") Then
                                Return True
                            Else
                                Return False
                            End If
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                            If (Destination = "O") Then
                                If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                    'ทั้งหมด
                                    Return True
                                ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                    'อื่นๆที่ไม่ใช่ FOB
                                    If (TypeBUy = "O") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                Else
                                    'FOB
                                    If (TypeBUy = "F") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End If
                            Else
                                Return False
                            End If
                        End If
                    Else
                        Return False
                    End If
                ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "O") Then
                    'Other
                    If (VesselNational = "O") Then
                        If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                            Return True
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                            If (Destination = "I") Then
                                Return True
                            Else
                                Return False
                            End If
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                            If (Destination = "O") Then
                                If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                    'ทั้งหมด
                                    Return True
                                ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                    'อื่นๆที่ไม่ใช่ FOB
                                    If (TypeBUy = "O") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                Else
                                    'FOB
                                    If (TypeBUy = "F") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End If
                            Else
                                Return False
                            End If
                        End If
                    Else
                        Return False
                    End If
                End If
            ElseIf (MAT.Rows(0).Item("MAT_ComputebuyerSeller") & "" = "I") Then
                'IRPC
                If (BuyerSeller = "Y") Then
                    If (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "A") Then
                        If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                            Return True
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                            If (Destination = "I") Then
                                Return True
                            Else
                                Return False
                            End If
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                            If (Destination = "O") Then
                                If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                    'ทั้งหมด
                                    Return True
                                ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                    'อื่นๆที่ไม่ใช่ FOB
                                    If (TypeBUy = "O") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                Else
                                    'FOB
                                    If (TypeBUy = "F") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End If
                            Else
                                Return False
                            End If
                        End If
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "T") Then
                        'Thai
                        If (VesselNational = "T") Then
                            If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                                Return True
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                                If (Destination = "I") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                                If (Destination = "O") Then
                                    If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                        'ทั้งหมด
                                        Return True
                                    ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                        'อื่นๆที่ไม่ใช่ FOB
                                        If (TypeBUy = "O") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    Else
                                        'FOB
                                        If (TypeBUy = "F") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    End If
                                Else
                                    Return False
                                End If
                            End If
                        Else
                            Return False
                        End If
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "O") Then
                        'Other
                        If (VesselNational = "O") Then
                            If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                                Return True
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                                If (Destination = "I") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                                If (Destination = "O") Then
                                    If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                        'ทั้งหมด
                                        Return True
                                    ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                        'อื่นๆที่ไม่ใช่ FOB
                                        If (TypeBUy = "O") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    Else
                                        'FOB
                                        If (TypeBUy = "F") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    End If
                                Else
                                    Return False
                                End If
                            End If
                        Else
                            Return False
                        End If
                    End If
                Else
                    Return False
                End If
            ElseIf (MAT.Rows(0).Item("MAT_ComputebuyerSeller") & "" = "O") Then
                'Other
                If (BuyerSeller <> "Y") Then
                    If (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "A") Then
                        If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                            Return True
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                            If (Destination = "I") Then
                                Return True
                            Else
                                Return False
                            End If
                        ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                            If (Destination = "O") Then
                                If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                    'ทั้งหมด
                                    Return True
                                ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                    'อื่นๆที่ไม่ใช่ FOB
                                    If (TypeBUy = "O") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                Else
                                    'FOB
                                    If (TypeBUy = "F") Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End If
                            Else
                                Return False
                            End If
                        End If
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "T") Then
                        'Thai
                        If (VesselNational = "T") Then
                            If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                                Return True
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                                If (Destination = "I") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                                If (Destination = "O") Then
                                    If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                        'ทั้งหมด
                                        Return True
                                    ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                        'อื่นๆที่ไม่ใช่ FOB
                                        If (TypeBUy = "O") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    Else
                                        'FOB
                                        If (TypeBUy = "F") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    End If
                                Else
                                    Return False
                                End If
                            End If
                        Else
                            Return False
                        End If
                    ElseIf (MAT.Rows(0).Item("MAT_ComputeNational") & "" = "O") Then
                        'Other
                        If (VesselNational = "O") Then
                            If (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "A") Then
                                Return True
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "T") Then
                                If (Destination = "I") Then
                                    Return True
                                Else
                                    Return False
                                End If
                            ElseIf (MAT.Rows(0).Item("MAT_ComputeDestinaation") & "" = "O") Then
                                If (Destination = "O") Then
                                    If (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "A") Then
                                        'ทั้งหมด
                                        Return True
                                    ElseIf (MAT.Rows(0).Item("MAT_ComputeBuy") & "" = "O") Then
                                        'อื่นๆที่ไม่ใช่ FOB
                                        If (TypeBUy = "O") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    Else
                                        'FOB
                                        If (TypeBUy = "F") Then
                                            Return True
                                        Else
                                            Return False
                                        End If
                                    End If
                                Else
                                    Return False
                                End If
                            End If
                        Else
                            Return False
                        End If
                    End If
                Else
                    Return False
                End If
            End If
        Else
            'ไม่คิดเงิน
            Return False
        End If
    End Function

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        '  Session("Value") = Nothing
        Session("StandardTimePlus") = 0
        Session("IRPCLiquid") = Nothing
        Session("CodeMATFree_SAP") = Nothing
        Session("Shifting") = Nothing
        Session("Status_Pay") = Nothing
        Session("ViewestimeteOld") = Nothing
        Session("Booking_no_ESTIMATE") = Nothing
        Session("EditBooking_Show") = Nothing
        Session("Owner_OLD_ESTIMATE") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('2');", True)
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        '  Session("Value") = Nothing
        Session("StandardTimePlus") = 0
        Session("IRPCLiquid") = Nothing
        Session("Shifting") = Nothing
        Session("Status_Pay") = Nothing
        Session("Booking_no_ESTIMATE") = Nothing
        Session("EditBooking_Show") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('B');", True)
    End Sub

    Private Function Gen_Sum(ByVal Net As String, ByVal Vat As String, ByVal WHT As String) As String
        Dim HTML As String = ""

        HTML = "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"

        HTML &= "<tr><td colspan='6'>&nbsp;</td></tr>"

        HTML &= "<tr>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>NET</font></td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>VAT</font></td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>WHITHOLDING</font>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#aeaeae;' align='center'><font class='White-Style' style='color:#000000'>ยอดเงินที่ต้องชำระ</font></td>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(Net, 2) & "</font></td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(Vat, 2) & "</font></td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(WHT, 2) & "</font>&nbsp;</td>"
        HTML &= "<td style='width:130px; background-color:#e7f9ff;' align='center'><font class='White-Style' style='color:#000000'>" & FormatNumber(((CDbl(Net) + CDbl(Vat)) - CDbl(WHT)).ToString("#############0.00"), 2) & "</font></td>"
        HTML &= "<td>&nbsp;</td>"
        HTML &= "</tr>"
        Session("SumTotal") = ((CDbl(Net) + CDbl(Vat)) - CDbl(WHT)).ToString("#############0.00")
        ViewState("Net_R") = Net
        ViewState("Vat_R") = Vat
        ViewState("WHT_R") = WHT
        HTML &= "</table>"
        Return HTML
    End Function


    Private Function PlusHourMinute(ByVal Original As String, ByVal Plus As String) As String
        Dim Hour As Integer = Original.Split(":")(0)
        Dim Minute As Integer = Original.Split(":")(1)
        Hour += CInt(Plus.Split(":")(0))
        Minute += CInt(Plus.Split(":")(1))
        If (Minute > 60) Then
            Hour += Minute / 60
            Minute = Minute Mod 60
        End If
        Return Hour.ToString("00") & ":" & Minute.ToString("00")
    End Function

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        Dim Vat As String = ""
        Dim dsConfig As DataSet = Session("Config")
        If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
            Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
        Else
            Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
        End If


        Dim dt As DataTable = CreateDataTableForReport()
        Dim dSITem As DataTable = ViewState("DSItem")
        For Each dr As DataRow In dSITem.Rows
            Dim dn As DataRow = dt.NewRow
            dn.Item("MATERIAL") = dr.Item("id")
            dn.Item("Text_line") = dr.Item("MAT_NAME")
            dn.Item("data1") = dr.Item("data1")
            dn.Item("data2") = dr.Item("data2")
            dn.Item("data3") = dr.Item("data3")
            dn.Item("data4") = dr.Item("data4")
            dt.Rows.Add(dn)
            dt.AcceptChanges()
        Next
        If dt.Rows.Count > 0 Then

            dt.Columns.Add("VAT", GetType(String))
            dt.Columns.Add("VAT_AMOUNT", GetType(String))
            dt.Columns.Add("NET", GetType(String))
            dt.Columns.Add("AMOUNT", GetType(String))
            dt.Columns.Add("Text_AMOUNT", GetType(String))
            dt.AcceptChanges()
            Dim NET As Double = 0.0
            For i As Integer = 0 To dt.Rows.Count - 1
                NET = NET + CDbl(CDbl(dt.Rows(i).Item("data4")).ToString("##############0.00"))
            Next
            Dim VAT_AMOUNT As String = ((NET * CDbl(Vat)) / 100).ToString("############.00")
            Dim AMOUNT As String = NET + CDbl(((NET * CDbl(Vat)) / 100).ToString("############.00"))
            Dim Text_AMOUNT As String = ConvertMoneyToText((NET + CDbl(((NET * CDbl(Vat)) / 100).ToString("############.00"))).ToString())
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim MAT As String = ""
                Try
                    MAT = CInt(dt.Rows(i).Item("MATERIAL"))
                Catch ex As Exception
                    MAT = dt.Rows(i).Item("MATERIAL")
                End Try
                dt.Rows(i).Item("MATERIAL") = MAT
                dt.Rows(i).Item("VAT") = Vat
                dt.Rows(i).Item("VAT_AMOUNT") = FormatNumber(VAT_AMOUNT, 2)
                dt.Rows(i).Item("NET") = FormatNumber(NET, 2)
                dt.Rows(i).Item("AMOUNT") = FormatNumber(AMOUNT, 2)
                dt.Rows(i).Item("Text_AMOUNT") = Text_AMOUNT
                Try
                    dt.Rows(i).Item("data1") = FormatNumber(dt.Rows(i).Item("data1"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data2") = FormatNumber(dt.Rows(i).Item("data2"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data3") = FormatNumber(dt.Rows(i).Item("data3"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data4") = FormatNumber(dt.Rows(i).Item("data4"), 2)
                Catch ex As Exception

                End Try
                dt.AcceptChanges()
            Next
            dsReport.Tables.Add(dt.Copy)
            dsReport.AcceptChanges()
            ReportName = "Actual_Report.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Actual_Report_Actual"

            If (Session("Booking_no_ESTIMATE") & "" = "") Then
                Session("Booking_no_ESTIMATE") = "00000"
            End If
            dt = clsGet.Get_BookingOneRecord(Session("Booking_no_ESTIMATE")).Tables(0)


            paraName = "SHIP;PERIOD;LAYTIME;AGENT;LOA;GRT;DWT;PO;WHARF;Booking_no;Estimate;Net;Vat;WHT;SUM;Product"
            If (dt.Rows.Count > 0) Then
                Dim PORT As String = dt.Rows(0).Item("PORT")
                Dim LAYTIME As String = ""
                Dim DateAP As String = dt.Rows(0).Item("Marketing_approve") & ""
                Try
                    DateAP = DateAP.Split(" ")(DateAP.Split(" ").Count - 1)
                    DateAP = DateAP.Split(":")(0)
                    LAYTIME = DateAP.Substring(6, 2) & " " & GetNameMonth(DateAP.Substring(4, 2)) & " " & CInt(DateAP.Substring(0, 4)) - 543
                Catch ex As Exception

                End Try


                Dim MAGEnt_Name As String = ""
                Dim Name() As String = lbAgent.Text.Split(":")(1).Split("-")
                For i As Integer = 1 To Name.Length - 1
                    MAGEnt_Name &= Name(i) & " "
                Next
                MAGEnt_Name = CInt(Name(0)) & "-" & MAGEnt_Name

                dt = clsGet.Get_BookingWharf(Session("Booking_no_ESTIMATE"))
                Dim WHARF As String = ""
                If (dt.Rows.Count > 0) Then
                    WHARF = dt.Rows(0).Item("Wharf")
                End If

                Dim PERIOD As String = ""
                PERIOD &= ""
                Dim PO As String = ""
                Try
                    PO = FormatNumber(ViewState("PO"), 2)
                Catch ex As Exception
                    PO = "0"
                End Try
                paraValue = lbShip.Text.Split(":")(1) & ";" & PERIOD & ";" & LAYTIME & ";" & MAGEnt_Name & ";" & lbLoA.Text.Split(":")(1) & ";" & lbGrt.Text.Split(":")(1) & ";" & lbdWT.Text.Split(":")(1) & ";" & lbPo.Text.Split(":")(1).Replace("MT", "") & " MT" & ";" & WHARF & ";" & Session("Booking_no_ESTIMATE") & ";ESTIMATE;" & ViewState("Net_R") & ";" & ViewState("Vat_R") & ";" & ViewState("WHT_R") & ";" & Session("SumTotal") & ";" & lbProductName.Text.Split(":")(1)
            Else
                If Session("Booking_no_ESTIMATE") = "00000" Then
                    Dim MAGEnt_Name As String = ""
                    Dim Name() As String = lbAgent.Text.Split(":")(1).Split("-")
                    For i As Integer = 1 To Name.Length - 1
                        MAGEnt_Name &= Name(i) & " "
                    Next
                    Try
                        MAGEnt_Name = CInt(Name(0)) & "-" & MAGEnt_Name
                    Catch ex As Exception

                    End Try

                    paraValue = lbShip.Text.Split(":")(1) & ";;;" & MAGEnt_Name & ";" & lbLoA.Text.Split(":")(1) & ";" & lbGrt.Text.Split(":")(1) & ";" & lbdWT.Text.Split(":")(1) & ";" & lbPo.Text.Split(":")(1).Replace("MT", "") & " MT" & ";;" & Session("Booking_no_ESTIMATE") & ";ESTIMATE;" & ViewState("Net_R") & ";" & ViewState("Vat_R") & ";" & ViewState("WHT_R") & ";" & Session("SumTotal") & ";" & lbProductName.Text.Split(":")(1)
                Else
                    paraValue = ";;;;;;;;;;;0;0;0;0;"
                End If
            End If

            Orientation = True
            reportType = "pdf"

            Try
                urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
            Catch ex As Exception
                Dim errortxt As String = ex.Message
            End Try
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบรายการ Invoice');", True)
        End If
    End Sub


    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Function ConvertMoneyToText(ByVal Money As String) As String
        Dim Digit As String = Money.Split(".")(0)
        Dim SubMoney As String
        Try
            SubMoney = Money.Split(".")(1)
        Catch ex As Exception
            SubMoney = "00"
        End Try
        Dim COuntLength As Int16 = Digit.Length

        Dim TxtMoney As String = ""

        For i As Integer = 0 To COuntLength - 1
            If (COuntLength > 7) Then
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2" : TxtMoney &= "สอง"
                    Case "1" : TxtMoney &= "หนึ่ง"
                End Select

                Select Case (COuntLength.ToString)
                    Case "13" : TxtMoney &= "ล้านล้าน"
                    Case "12" : TxtMoney &= "แสนล้าน"
                    Case "11" : TxtMoney &= "หมื่นล้าน"
                    Case "10" : TxtMoney &= "พันล้าน"
                    Case "9" : TxtMoney &= "ร้อยล้าน"
                    Case "8" : TxtMoney &= "สิบล้าน"
                End Select
            Else
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "ยี่"
                        Else
                            TxtMoney &= "สอง"
                        End If

                    Case "1"
                        If (COuntLength.ToString() = "1") Then
                            TxtMoney &= "เอ็ด"
                        ElseIf (COuntLength.ToString() = "2") Then
                            TxtMoney &= ""
                        Else
                            TxtMoney &= "หนึ่ง"
                        End If
                End Select

                Select Case (COuntLength.ToString)
                    Case "7" : TxtMoney &= "ล้าน"
                    Case "6" : TxtMoney &= "แสน"
                    Case "5" : TxtMoney &= "หมื่น"
                    Case "4" : TxtMoney &= "พัน"
                    Case "3" : TxtMoney &= "ร้อย"
                    Case "2" : TxtMoney &= "สิบ"
                    Case "1" : TxtMoney &= "บาท"
                End Select
            End If
            Select Case (COuntLength.ToString())
                Case "13"
                    Digit = CInt(Digit) Mod 1000000000000
                Case "12"
                    Digit = CInt(Digit) Mod 100000000000
                Case "11"
                    Digit = CInt(Digit) Mod 10000000000
                Case "10"
                    Digit = CInt(Digit) Mod 1000000000
                Case "9"
                    Digit = CInt(Digit) Mod 100000000
                Case "8"
                    Digit = CInt(Digit) Mod 10000000
                Case "7"
                    Digit = CInt(Digit) Mod 1000000
                Case "6"
                    Digit = CInt(Digit) Mod 100000
                Case "5"
                    Digit = CInt(Digit) Mod 10000
                Case "4"
                    Digit = CInt(Digit) Mod 1000
                Case "3"
                    Digit = CInt(Digit) Mod 100
                Case "2"
                    Digit = CInt(Digit) Mod 10
                Case "1"
                    Digit = 0
            End Select
            If (Digit <> 0) Then
                COuntLength = Digit.Length
            Else
                COuntLength = 0
            End If

        Next
        If (SubMoney <> "00") Then
            If (SubMoney.Length = 2) Then
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบ"
                    Case "8" : TxtMoney &= "แปดสิบ"
                    Case "7" : TxtMoney &= "เจ็ดสิบ"
                    Case "6" : TxtMoney &= "หกสิบ"
                    Case "5" : TxtMoney &= "ห้าสิบ"
                    Case "4" : TxtMoney &= "สี่สิบ"
                    Case "3" : TxtMoney &= "สามสิบ"
                    Case "2" : TxtMoney &= "ยี่สิบ"
                    Case "1" : TxtMoney &= "สิบ"
                End Select
                Select Case (SubMoney.Substring(1, 1))
                    Case "9" : TxtMoney &= "เก้าสตางค์"
                    Case "8" : TxtMoney &= "แปดสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสตางค์"
                    Case "6" : TxtMoney &= "หกสตางค์"
                    Case "5" : TxtMoney &= "ห้าสตางค์"
                    Case "4" : TxtMoney &= "สี่สตางค์"
                    Case "3" : TxtMoney &= "สามสตางค์"
                    Case "2" : TxtMoney &= "สองสตางค์"
                    Case "1"
                        If (SubMoney.Length = 2) Then
                            If (SubMoney.Substring(0, 1) = "0") Then
                                TxtMoney &= "หนึ่งสตางค์"
                            Else
                                TxtMoney &= "เอ็ดสตางค์"
                            End If

                        Else
                            TxtMoney &= "หนึ่งสตางค์"
                        End If
                End Select
            Else
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบสตางค์"
                    Case "8" : TxtMoney &= "แปดสิบสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสิบสตางค์"
                    Case "6" : TxtMoney &= "หกสิบสตางค์"
                    Case "5" : TxtMoney &= "ห้าสิบสตางค์"
                    Case "4" : TxtMoney &= "สี่สิบสตางค์"
                    Case "3" : TxtMoney &= "สามสิบสตางค์"
                    Case "2" : TxtMoney &= "ยี่สิบสตางค์"
                    Case "1" : TxtMoney &= "สิบสตางค์"
                End Select
            End If
        End If
        Return TxtMoney
    End Function

    Private Function CreateDataTableForReport()
        Dim dt As New DataTable
        dt.Columns.Add("MATERIAL", GetType(String))
        dt.Columns.Add("Text_line", GetType(String))
        dt.Columns.Add("data1", GetType(String))
        dt.Columns.Add("data2", GetType(String))
        dt.Columns.Add("data3", GetType(String))
        dt.Columns.Add("data4", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function
End Class